package com.changgou.item.service.Impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import entity.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PageServiceImplMy implements PageService {

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    //TemplateEngine 和Context 的配合，我们可以使用thymeleaf模版来生产html文件
    @Autowired
    private TemplateEngine templateEngine;

    //生成静态页的文件夹路径
    @Value("${pagepath}")
    private String pagepath;

    /***
     * 构建数据模型
     * @param spuId
     * @return
     */

    private Map<String,Object> buildDataModel(Long spuId){
        Map<String,Object> dataMap = new HashMap<>();
        //通过spuid拿到spu的数据
        Result<Spu> result = spuFeign.findById(spuId);
        Spu spu = result.getData();
        //获取三级分类
        //每次查询到是一个对象category
        dataMap.put("category1",categoryFeign.findById(spu.getCategory1Id()).getData());
        dataMap.put("category2",categoryFeign.findById(spu.getCategory2Id()).getData());
        dataMap.put("category3",categoryFeign.findById(spu.getCategory3Id()).getData());
        dataMap.put("spu",spu);
        //获取图片
        if (spu.getImages() != null){
            dataMap.put("imageList",spu.getImages().split(","));
        }
        //获取所有可选规格
        dataMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));
        //根据spuid查询到sku集合
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        Result<List<Sku>> skuFeignList = skuFeign.findList(sku);
        dataMap.put("skuList",skuFeignList.getData());
        return dataMap;
    }

    /**
     * 生成静态页
     * @param spuId
     */
    @Override

    public void createPageHtml(Long spuId) {
        //上下文  //Context是给thymeleaf模版提供变量的。 thymeleaf包下的
        Context context = new Context();
        Map<String, Object> dataModel = buildDataModel(spuId);
        context.setVariables(dataModel);
        //2,准备文件
        File dir = new File(pagepath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //创建静态页面
        File dest = new File(dir, spuId + ".html");
        //3,生成页面

        try {
            PrintWriter writer = new PrintWriter(dest,"UTF-8");

            templateEngine.process("item",context,writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
